package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.user.CUserReturnVo;
import com.wanxi.education.model.vo.user.CUserVo;
import com.wanxi.education.model.vo.user.FindByIdCUserVo;
import com.wanxi.education.model.vo.user.UpdateCUserVo;
import com.wanxi.education.service.CUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: YanZhengYi
 * @Description:C端用户管理页面
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "C端用户管理")
@RestController
@RequestMapping("/back/cUser")
public class CUserController {
    @Autowired
    private CUserService cUserService;
    @GetMapping("/findPage")
    @ApiOperation("查询所有的用户数据")
    public CommonResult findPage(CUserVo cUserVo){
        PageInfo<CUserReturnVo> cUserReturnVo = cUserService.cUserFindPage(cUserVo);
        return CommonResult.success(cUserReturnVo);
    }

    @PostMapping("/update")
    @ApiOperation("更新数据")
    public CommonResult update(@RequestBody UpdateCUserVo updateCUserVo){
        Integer result = cUserService.cUserUpdate(updateCUserVo);
        if (result==null)return CommonResult.failure("更新失败,电话号码或身份证号重复");
        return CommonResult.success("success");
    }

    @PostMapping("/delete")
    @ApiOperation("删除一行")
    public CommonResult delete(Long id){
        Integer result =  cUserService.delete(id);
        if (result==null)return CommonResult.failure("删除失败");
        return CommonResult.success("success");
    }

    @PostMapping("/insert")
    @ApiOperation("添加数据")
    public CommonResult insert(@RequestBody UpdateCUserVo updateCUserVo){
        String result = cUserService.cUserInsert(updateCUserVo);
        if (!"success".equals(result))return CommonResult.failure(result);
        return CommonResult.success("success");
    }

    @GetMapping("/findById")
    @ApiOperation("通过id回显数据")
    public CommonResult findById(Long id){
        FindByIdCUserVo findByIdCUserVo = cUserService.findById(id);
        return CommonResult.success(findByIdCUserVo);
    }
}
